<html>
<head><title>CS143 Project 1B by Amir Mirsalimi</title></head>
<body>
<p> Type an SQL query in the following box:
<p>
<form method="GET">
<textarea name="query" cols="60" rows="8">
</textarea>
<input type="submit" value="Submit">
</form>
</p>

<?php
$x = $_GET["query"];
$db_connection = mysql_connect("localhost", "cs143", "");
if(!$db_connection) {
    $errmsg = mysql_error($db_connection);
    print "Connection failed.";
    exit(1);
}
print "Showing results for: ".$x;
echo "<br />";
echo "<br />";
mysql_select_db("CS143",$db_connection);
$rs = mysql_query($x,$db_connection);
$length = mysql_num_fields($rs);

echo mysql_field_name($rs, 0);
for( $i=1; $i<$length; $i++){
	echo " , \t ";
	echo mysql_field_name($rs, $i);
}
echo "<br />";

while($row = mysql_fetch_row($rs)) {
	echo $row[0];
	for( $i=1; $i<$length; $i++){
		if($row[$i])
			echo " , \t ";
		echo $row[$i];
	}
	echo "<br />";
}


mysql_close($db_connection);
?>


</body>
</html>